﻿using System;
using System.Collections.Generic;
namespace CoinsLibrary
{
    public static class Aex
    {
        public static string MainUrl = "https://api.bit.cc/";
        public static string APIKEY = "";
        public static string APISKEY = "";
        public static string USERID = "";

        public static void Init(string key,string skey,string uid)
        {
            APIKEY = key;
            APISKEY = skey;
            USERID = uid;
        }
        public static string GetDepth(string coin, string mk)
        {
            string resource = "depth.php?c=" + coin + "&mk_type=" + mk;
            return Http.HttpGet(MainUrl,resource);
        }

        public static string SubmitOrder(string type, string market, double price, double amount, string coinname)
        {
            var time = DHTools.GetTimeStamp();
            var sign = CreateSign(time + "");
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("key", APIKEY);
            keyValuePairs.Add("time", time + "");
            keyValuePairs.Add("md5", sign);
            keyValuePairs.Add("type", type);
            keyValuePairs.Add("mk_type", market);
            keyValuePairs.Add("price", price + "");
            keyValuePairs.Add("amount", amount + "");
            keyValuePairs.Add("coinname", coinname);
            return Http.HttpPost(MainUrl, "submitOrder.php", keyValuePairs);
        }

        public static string GetOrderList(string mk_type, string coinname)
        {
            var time = DHTools.GetTimeStamp();
            var sign = CreateSign(time + "");
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("key", APIKEY);
            keyValuePairs.Add("time", time + "");
            keyValuePairs.Add("md5", sign);
            keyValuePairs.Add("mk_type", mk_type);
            keyValuePairs.Add("coinname", coinname);
            return Http.HttpPost(MainUrl, "getOrderList.php", keyValuePairs);
        }
        public static string GetBalance()
        {
            var time = DHTools.GetTimeStamp();
            var sign = CreateSign(time + "");
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("key", APIKEY);
            keyValuePairs.Add("time", time + "");
            keyValuePairs.Add("md5", sign);
            return Http.HttpPost(MainUrl, "getMyBalance.php", keyValuePairs);
        }
        public static string CancelOrder(string mk_type, string coinname,string order_id)
        {
            var time = DHTools.GetTimeStamp();
            var sign = CreateSign(time + "");
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("key", APIKEY);
            keyValuePairs.Add("time", time + "");
            keyValuePairs.Add("md5", sign);
            keyValuePairs.Add("mk_type", mk_type);
            keyValuePairs.Add("order_id", order_id);
            keyValuePairs.Add("coinname", coinname);
            return Http.HttpPost(MainUrl, "cancelOrder.php", keyValuePairs);
        }
        static string CreateSign(string time)
        {
            string sign = APIKEY + "_" + USERID + "_" + APISKEY + "_" + time;
            return DHTools.GetMd5(sign);
        }
    }
}
